package com.jy.horny.function.home.viewmodel

import com.framework.network.remote.NetworkObserver
import com.framework.network.remote.endRequest
import com.framework.network.remote.error
import com.framework.network.remote.requestObserver
import com.framework.network.remote.startRequest
import com.framework.network.remote.success
import com.jy.horny.base.BaseVMViewModel
import com.jy.horny.base.HttpRequestListener
import com.jy.horny.base.Request
import com.jy.horny.model.HomeListModel

class HomeViewModel : BaseVMViewModel() {
    fun getData(listener: HttpRequestListener<HomeListModel>) {
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postHomeList(map)?.requestObserver(
                    NetworkObserver<HomeListModel>().startRequest {
                        showLoading()
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        hideLoading()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }

    fun checkLogOut(listener: HttpRequestListener<String>){
        request(listener, request = object :Request{
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postAuthCheckLogOut(map)?.requestObserver(
                    NetworkObserver<String>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )
            }

        })

    }
}